package be.android.forap.dataprovider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.text.TextUtils;
import android.util.Log;

public class ContactUtil {
	public static String generateUniqueGlobalID(){
		UUID u = UUID.randomUUID();
		return u.toString();
	}

	public static ArrayList<Map<String, String>> getContactsWithPhone(Context context){
		ArrayList<Map<String, String>> mPeopleList = new ArrayList<Map<String, String>>();

		ContentResolver resolver = context.getContentResolver();

		//Cursor people = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

		String projection[] = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
				ContactsContract.CommonDataKinds.Phone.NUMBER,
				ContactsContract.CommonDataKinds.Phone.TYPE};


		Cursor people = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
		while (people.moveToNext()){
			String contactName = people.getString(people.getColumnIndex(
					ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
			//ContactsContract.Contacts.DISPLAY_NAME));

			String phoneNumber = people.getString(people.getColumnIndex(
					ContactsContract.CommonDataKinds.Phone.NUMBER));
			String numberType = people.getString(people.getColumnIndex(
					ContactsContract.CommonDataKinds.Phone.TYPE));



			/*String contactId = people.getString(people.getColumnIndex(
					ContactsContract.Contacts._ID) );
			String hasPhone = people.getString(people.getColumnIndex(
					ContactsContract.Contacts.HAS_PHONE_NUMBER));
			if (Integer.parseInt(hasPhone) > 0 ){
				// You know have the number so now query it like this
				Cursor phones = resolver.query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
						null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
						null, null);
				while (phones.moveToNext()) {

					//store numbers and display a dialog letting the user select which.
					String phoneNumber = phones.getString(
							phones.getColumnIndex(
									ContactsContract.CommonDataKinds.Phone.NUMBER));

					/*String numberType = phones.getString(phones.getColumnIndex(
							ContactsContract.CommonDataKinds.Phone.TYPE));*/

			Map<String, String> NamePhoneType = new HashMap<String, String>();

			NamePhoneType.put("Name", contactName);
			NamePhoneType.put("Phone", phoneNumber);
			if(TextUtils.isEmpty(numberType)){
				NamePhoneType.put("Type", "Not defined");
			}
			else {
				if(numberType.equals("0"))
					NamePhoneType.put("Type", "Work");
				else
					if(numberType.equals("1"))
						NamePhoneType.put("Type", "Home");
					else if(numberType.equals("2"))
						NamePhoneType.put("Type",  "Mobile");
					else
						NamePhoneType.put("Type", "Other");
			}

			//Then add this map to the list.
			mPeopleList.add(NamePhoneType);
			//}
			//phones.close();
			//	}
		}
		people.close();
		return mPeopleList;
	}

	public static ArrayList<Map<String, String>> getContactsInfo(Context context, String phoneNumber){
		ArrayList<Map<String, String>> mPeopleList = new ArrayList<Map<String, String>>();


		ContentResolver resolver = context.getContentResolver();

		//Cursor people = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

		String projection[] = {
				ContactsContract.CommonDataKinds.Phone._ID,
				ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};


		Cursor people = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
				ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ phoneNumber, null, null);

		String contactId = people.getString(people.getColumnIndex(
				ContactsContract.Contacts._ID) );
		while (people.moveToNext()){
			Map<String, String> NamePhoneEmail = new HashMap<String, String>();

			NamePhoneEmail.put("Phone", phoneNumber);

			String contactName = people.getString(people.getColumnIndex(
					ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
			//ContactsContract.Contacts.DISPLAY_NAME));
			NamePhoneEmail.put("Name", contactName);

			Cursor emails = resolver.query(
					ContactsContract.CommonDataKinds.Email.CONTENT_URI,
					new String[] {ContactsContract.CommonDataKinds.Email.DATA1},
					ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,
					null, null);

			if(emails.moveToFirst()){
				String email = emails.getString(emails.getColumnIndex(
						ContactsContract.CommonDataKinds.Email.DATA1));
				NamePhoneEmail.put("Email" , email);
			}

			/*String contactId = people.getString(people.getColumnIndex(
					ContactsContract.Contacts._ID) );
			String hasPhone = people.getString(people.getColumnIndex(
					ContactsContract.Contacts.HAS_PHONE_NUMBER));
			if (Integer.parseInt(hasPhone) > 0 ){
				// You know have the number so now query it like this
				Cursor phones = resolver.query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
						null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
						null, null);
				while (phones.moveToNext()) {

					//store numbers and display a dialog letting the user select which.
					String phoneNumber = phones.getString(
							phones.getColumnIndex(
									ContactsContract.CommonDataKinds.Phone.NUMBER));

					/*String numberType = phones.getString(phones.getColumnIndex(
							ContactsContract.CommonDataKinds.Phone.TYPE));*/


			//Then add this map to the list.
			mPeopleList.add(NamePhoneEmail);
			//}
			//phones.close();
			//	}
		}
		people.close();
		return mPeopleList;
	}

	public static String getContactName(Context context, String phoneNumber){

		Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

		// Query the filter URI
		String[] projection = new String[]{ PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER};
		Cursor people = context.getContentResolver().query(uri, projection, null,null,null);

		Log.d("EXT","l "+people.getCount());
		String contactName = "";
		while(people.moveToNext()){
			contactName = people.getString(people.getColumnIndex(
					ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

			Log.d("EXT","l "+contactName);
		}
		return contactName;

	}

}
